<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2023/5/9
  Time: 15:46
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
    <link rel="icon" href="<c:url value="/static/images/favicon.ico"/>">
    <link rel="stylesheet" href="<c:url value="/static/lib/layui-v2.5.5/css/layui.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/lib/font-awesome-4.7.0/css/font-awesome.min.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/layuimini.css?v=2.0.1"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/themes/default.css"/>" media="all">
    <link rel="stylesheet" href="<c:url value="/static/css/public.css"/>" media="all">
    <title>添加用户</title>
</head>
<div class="layuimini-main">

    <div class="layui-form layuimini-form">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户ID</label>
            <div class="layui-input-block">
                <input type="text" name="userId" lay-verify="required" lay-reqtext="ID不能为空" placeholder="请输入ID" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="nickName" value="小龙人儿" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="1" title="男" checked="">
                <input type="radio" name="gender" value="0" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">手机</label>
            <div class="layui-input-block">
                <input type="tel" name="phone" lay-verify="required" lay-reqtext="手机不能为空" placeholder="请输入手机" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
                <input type="email" name="email" lay-verify="required" placeholder="请输入邮箱" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">生日</label>
            <div class="layui-input-inline">
                <input type="date" name="birthday" id="birthday" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" id="password" placeholder="请输入密码" autocomplete="off" lay-verify="required"
                       class="layui-input admin-input" value="123456Qs">
                <tip>密码长度不小于8位，包含“数字,大写字母,小写字母”中的至少三种~</tip>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
</div>
<script src="<c:url value="/static/lib/layui-v2.5.5/layui.js"/>" charset="utf-8"></script>
<script src="<c:url value="/static/js/lay-config.js?v=2.0.0"/>" charset="utf-8"></script>
<script>
    // 密码格式检查
    function checkPassword(pwd) {
        // 格式判断
        var regUpLetter = /[A-Z]/
        var regLowLetter = /[a-z]/
        var regNumber = /[0-9]/
        var complex = 0;
        if (regUpLetter.test(pwd)) {
            ++complex;
        }
        if (regLowLetter.test(pwd)) {
            ++complex;
        }
        if (regNumber.test(pwd)) {
            ++complex;
        }
        return !(complex < 3 || pwd.length < 8);
    }
    layui.use(['form', 'table'], function () {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            // laydate = layui.laydate,
            $ = layui.$;
        //日期
        // laydate.render({
        //     elem: '#birthday',
        //     format: 'yyyy-MM-dd',
        // });

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        // 监听提交
        form.on('submit(saveBtn)', function (data) {
            if(data.field.phone.length!==11){
                layer.msg("联系方式十一位");
                return false;
            }
            if(!checkPassword(data.field.password)){
                layer.msg("密码格式错误");
                return false;
            }
            var index = layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            }, function () {
                var user = data.field
                $.ajax({
                    url: "/user/addUser",
                    data: JSON.stringify(user),
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    type: "POST",
                    success:function(res){
                        if(res.status===200){
                            var seIndex=layer.alert(
                                res.message,
                                {
                                    title:'提示信息'
                                },
                                function (){
                                    layer.close(seIndex);
                                });
                        }else{
                            layer.msg(res.message,{icon:0,time:1000});
                            layer.close(index);
                        }
                        // 关闭弹出层
                       /* layer.close(parentIndex);*/
                    },
                    error:function(e){
                        console.log(e,e.message);
                        alert('error');
                    }
                });

            });


            return false;
        });

    });
</script>